package cn.desk.dialog.entity;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.apache.struts2.ServletActionContext;

import cn.desk.commons.entity.BaseEntity;

/**
 * 对话框
 * 
 * @author Administrator
 * 
 */
@Entity(name = "t_dialog")
public class Dialog extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	private String id;
	/**
	 * 对话框的基本内容
	 */
	private String content;
	/**
	 * 标题
	 */
	private String title;
	/**
	 * 宽度
	 */
	private Integer width;
	/**
	 * 高度
	 */
	private Integer height;
	/**
	 * 自动打开
	 */
	private Integer autoOpen ;
	/**
	 * 自动打开
	 */
	public static final int AUTO_OPEN_TRUE = 1;
	/**
	 * 不自动打开
	 */
	public static final int AUTO_OPEN_FALSE = 0;
	/**
	 * 是否可变大小
	 */
	private Integer resizable ;
	/**
	 * 可变大小
	 */
	public static final int RESIZABLE_TRUE = 1;
	/**
	 * 不可变大小
	 */
	public static final int RESIZABLE_FALSE = 0;

	/**
	 * 是否模态
	 */
	private Integer modal ;
	/**
	 * 模态对话框
	 */
	public static final int MODAL_TRUE = 1;
	/**
	 * 非模态对话框
	 */
	public static final int MODAL_FALSE = 0;
	/**
	 * 是否分页
	 */
	private Integer pager;
	/**
	 * 分页
	 */
	public static final int PAGER_TRUE = 1;
	/**
	 * 不分页
	 */
	public static final int PAGER_FALSE = 0;

	/**
	 * 按钮
	 */
	@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, mappedBy = "dialog")
	private Set<Button> buttons;

	public String getContent() {
		return content;
	}

	public String getDialogContent() {
		StringBuffer sb = new StringBuffer();
		sb.append(content);
		if (pager != null) {
			if (pager.intValue() == PAGER_TRUE) {
				Object pager = ServletActionContext.getRequest().getSession()
						.getAttribute("PAGER");
				if (pager != null) {
					sb.append(String.valueOf(pager));
				}
			}
		}
		return sb.toString();
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getAutoOpen() {
		return autoOpen;
	}

	public void setAutoOpen(Integer autoOpen) {
		this.autoOpen = autoOpen;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public void setButtons(Set<Button> buttons) {
		this.buttons = buttons;
	}

	// public void setButtons(List<Button> buttons) {
	// Set<Button> set = new HashSet<Button>();
	// set.addAll(buttons);
	// this.buttons = set;
	// }

	@Override
	@Transient
	public Object getValue() {
		return getId();
	}

	@Override
	@Transient
	public String getDisplayName() {
		return "对话框";
	}

	@Transient
	public String getEvalValue() {
		StringBuilder sb = new StringBuilder();
		sb.append("$('#");
		sb.append(getId());
		sb.append("').dialog({");
		if (autoOpen != null) {
			sb.append("autoOpen:");
			sb.append(autoOpen.intValue() == AUTO_OPEN_TRUE);
			sb.append(",");
		}
		if (resizable != null) {
			sb.append("resizable:");
			sb.append(resizable.intValue() == RESIZABLE_TRUE);
			sb.append(",");
		}
		if (modal != null) {
			sb.append("modal:");
			sb.append(modal.intValue() == MODAL_TRUE);
			sb.append(",");
		}
		if (width != null) {
			sb.append("width : ");
			sb.append(width);
			sb.append(",");
		}
		if (height != null) {
			sb.append("height : ");
			sb.append(height);
			sb.append(",");
		}
		if (title != null) {
			sb.append("title : '");
			sb.append(title);
			sb.append("',");
		}
		if (buttons != null) {
			if (buttons.size() > 0) {
				sb.append("buttons:{");
				for (Button but : buttons) {
					sb.append("\"");
					sb.append(but.getName());
					sb.append("\":");
					sb.append(but.getFunc());
					sb.append(",");
				}
				sb.deleteCharAt(sb.length() - 1);
				sb.append("},");
			}
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("});");
		return sb.toString();
	}

	public Integer getModal() {
		return modal;
	}

	public String getDisplayModal() {
		return null;
	}

	public void setModal(Integer modal) {
		this.modal = modal;
	}

	public Integer getResizable() {
		return resizable;
	}

	public void setResizable(Integer resizable) {
		this.resizable = resizable;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((autoOpen == null) ? 0 : autoOpen.hashCode());
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result + ((height == null) ? 0 : height.hashCode());
		result = prime * result + ((modal == null) ? 0 : modal.hashCode());
		result = prime * result
				+ ((resizable == null) ? 0 : resizable.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + ((width == null) ? 0 : width.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Dialog other = (Dialog) obj;
		if (autoOpen == null) {
			if (other.autoOpen != null)
				return false;
		} else if (!autoOpen.equals(other.autoOpen))
			return false;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (height == null) {
			if (other.height != null)
				return false;
		} else if (!height.equals(other.height))
			return false;
		if (modal == null) {
			if (other.modal != null)
				return false;
		} else if (!modal.equals(other.modal))
			return false;
		if (resizable == null) {
			if (other.resizable != null)
				return false;
		} else if (!resizable.equals(other.resizable))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (width == null) {
			if (other.width != null)
				return false;
		} else if (!width.equals(other.width))
			return false;
		return true;
	}

	public Integer getPager() {
		return pager;
	}

	public void setPager(Integer pager) {
		this.pager = pager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public Set<Button> getButtons() {
		return buttons;
	}

}
